﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace Sbook.AdminIstrator
{
    public partial class CreateAdmin : System.Web.UI.Page
    {
        protected static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Data.Mysession.isLogin)
                {
                    BindListUserAdmin();
                }
                else
                {
                    Response.Redirect("/AdminIstrator/Login.aspx");
                }
            }
        }

        private void BindListUserAdmin()
        {
            grdAdmin.DataSource = Business.UserService.User_GetByAll().FindAll(u => u.isAdmin == "1");
            grdAdmin.DataBind();
        }
        protected void Add_Click(object sender, EventArgs e)
        {
            Insert = true;
            pnViewAdmin.Visible = false;
            pnUpdateAdmin.Visible = true;
        }
        protected void Save_Click(object sender, EventArgs e)
        {
            if (Insert)
            {
                Data.UserInfo obj = new Data.UserInfo();
                if (txtUser.Text == "")
                    lblUser.Text = "User not to empty";
                if (txtPass.Text == "")
                    lblPass.Text = "Password not to empty";
                if (txtFullName.Text == "")
                    lblFullName.Text = "Fullname not to empty";
                if (txtRePass.Text == "")
                    lblRePass.Text = "Confirm Pass not to empty";
                if (txtRePass.Text != "" && txtUser.Text != "" && txtFullName.Text != "" && txtPass.Text != "")
                {
                    if (txtPass.Text != txtRePass.Text)
                        lblRePass.Text = "Confirm Pass not equals Password";
                    
                    else
                    {
                        if (Business.UserService.User_GetByAll().FindAll(u => u.Username == txtUser.Text).Count > 0)
                        {
                            lblUser.Text = "User ready exists";
                        }
                        else
                        {
                            if (!this.IsEmail(txtEmail.Text))
                            {
                                lblEmail.Text = "Email format failed";
                            }
                            else
                            {
                                obj.isAdmin = "1";
                                obj.Username = txtUser.Text;
                                obj.Fullname = txtFullName.Text;
                                obj.Pass = txtPass.Text;
                                obj.Email = txtEmail.Text;
                                obj.Phone = txtPhone.Text;
                                obj.Address = "";
                                obj.AddressTo = "";
                                obj.Class = "";
                                obj.FullNameTo = "";
                                obj.MailTo = "";
                                obj.PhoneTo = "";
                                obj.Provenumber = "";
                                obj.Status = "1";
                                obj.UId = "";
                                obj.University = "";
                                Business.UserService.User_Insert(obj);
                                BindListUserAdmin();
                                pnUpdateAdmin.Visible = false;
                                pnViewAdmin.Visible = true;
                            }
                        }
                    }
                }
            }
        }
        //Regex Email

       
            public const string MatchEmailPattern =
                      @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
               + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
				[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
               + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
				[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
               + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

            
            private  bool IsEmail(string email)
            {
                if (email != null) return Regex.IsMatch(email, MatchEmailPattern);
                else return false;
            }
              
        protected void Cancel_Click(object sender, EventArgs e)
        {
            BindListUserAdmin();
            pnViewAdmin.Visible = true;
            pnUpdateAdmin.Visible = false;
        }
    }
}